package cn.danger.com.activity;

import android.content.Intent;
import android.os.Bundle;

import cn.danger.com.fragment.BackHandledFragment;
import cn.danger.com.fragment.BackHandledInterface;
import cn.danger.com.fragment.memo.AddMemoFragment;
import cn.danger.com.fragment.memo.MemoDetailFragment;
import cn.danger.com.fragment.schedule.AddScheduleFragment;
import cn.danger.com.fragment.user.ChooseColorFragment;
import cn.danger.com.fragment.user.LoginFragment;
import cn.danger.com.fragment.user.UserInfoFragment;

/**
 * Created by 俊 on 2016/12/31.
 */
public class CommonActivity extends BaseActivity implements BackHandledInterface {

    private BackHandledFragment selectedFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common);

        bindView();
        bindEvent();

    }

    @Override
    public void bindView() {
        Intent intent = getIntent();
        if (intent.getStringExtra("target").equals("login")) {
            getFragmentManager().beginTransaction().addToBackStack(null).add(R.id.container, new LoginFragment()).commit();
        } else if (intent.getStringExtra("target").equals("userInfo")) {
            getFragmentManager().beginTransaction().addToBackStack(null).add(R.id.container, new UserInfoFragment()).commit();
        } else if (intent.getStringExtra("target").equals("color")) {
            Bundle bundle = new Bundle();
            bundle.putString("type", intent.getStringExtra("type"));
            ChooseColorFragment fragment = new ChooseColorFragment();
            fragment.setArguments(bundle);
            getFragmentManager().beginTransaction().addToBackStack(null).add(R.id.container, fragment).commit();
        } else if (intent.getStringExtra("target").equals("add_memo")) {
            getFragmentManager().beginTransaction().addToBackStack(null).add(R.id.container, new AddMemoFragment()).commit();
        } else if (intent.getStringExtra("target").equals("memo_detail")) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("memo", intent.getSerializableExtra("memo"));
            MemoDetailFragment fragment = new MemoDetailFragment();
            fragment.setArguments(bundle);
            getFragmentManager().beginTransaction().addToBackStack(null).add(R.id.container, fragment).commit();
        } else if (intent.getStringExtra("target").equals("add_schedule")) {
            AddScheduleFragment fragment = new AddScheduleFragment();
            if (intent.getSerializableExtra("schedule") != null) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("schedule", intent.getSerializableExtra("schedule"));
                fragment.setArguments(bundle);
            }
            getFragmentManager().beginTransaction().addToBackStack(null).add(R.id.container, fragment).commit();
        }

    }

    @Override
    public void bindEvent() {

    }

    @Override
    public void onBackPressed() {
        if (selectedFragment == null || !selectedFragment.onBackPressed()) {
            if (getFragmentManager().getBackStackEntryCount() == 1) {
                finish();
            } else {
                getFragmentManager().popBackStack();
            }
        }
    }

    @Override
    public void setSelectedFragment(BackHandledFragment selectedFragment) {
        this.selectedFragment = selectedFragment;
    }
}